import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:jyofservice/util/SlideRoute.dart';
import 'package:jyofservice/util/webconfig.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'Mine.dart';
import 'UpdatePage.dart';
import 'jicha_page.dart';

class SFHomePage extends StatefulWidget {
  @override
  _SFHomePageState createState() => _SFHomePageState();
}

class _SFHomePageState extends State<SFHomePage> with WidgetsBindingObserver {
  final GlobalKey<JiChaPageState> globalKeyJC = GlobalKey<JiChaPageState>();
  late Timer _timer;
  DateTime? _lastPressedAt;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  Widget build(BuildContext context) {

    return UpdatePage(WillPopScope(
      onWillPop: () async {
        if (_lastPressedAt == null ||
            DateTime.now().difference(_lastPressedAt!) > Duration(seconds: 2)) {
          //两次点击间隔超过1秒则重新计时
          _lastPressedAt = DateTime.now();
          Fluttertoast.showToast(msg: '连续按两次返回桌面');
          return false;
        } else {
          exit(0);
          _timer.cancel();
          return true;
        }
      },
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.white,
          centerTitle: true,
          title: Text(WebConfig().APPNAME,
              style: TextStyle(
                  color: Colors.black, fontSize: ScreenUtil().setSp(45))),
          actions: <Widget>[
            TextButton(
              child: Icon(
                Icons.person,
                size: 30,
              ),
              onPressed: () {
                Navigator.of(context)
                    .push(SlideRoute().createRoute(MinePage()));
              },
            )
          ], systemOverlayStyle: SystemUiOverlayStyle.dark,
        ),
        body: Container(
            color: Colors.white,
            child: Column(
              children: <Widget>[
                Container(
                  child: Image.asset(
                    'images/banner.png',
                    fit: BoxFit.fill,
                  ),
                  width: ScreenUtil().setWidth(750),
                  height: ScreenUtil().setHeight(400),
                ),
                //稽查管理
                getJiChaPage(),

//              LeaderPage(),
              ],
            ),
          ),

      ),
    ));
  }

  Widget getJiChaPage() {
    return JiChaPage(
      key: globalKeyJC,
    );
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}
